//
//  NetworkTools.swift
//  swift网络工具类
//
//  Created by 李学林 on 15/12/6.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

enum XLRequestMethod: String {
    case GET = "GET"
    case POST = "POST"
}
class NetworkTools: AFHTTPSessionManager {
    
    
    // 给通过的返回 闭包 定义一个别名
    typealias HMNetworkToolsCallBack = (response: AnyObject?, error: NSError?)->()
    
    static let shareManager :NetworkTools = {
        let tools = NetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools;
    
    }()
    
    func request(method:XLRequestMethod, urlString:String ,para: AnyObject?,callback: (response: AnyObject?, error: NSError?)->()){
    
        let successCallBack = {(dataTask: NSURLSessionDataTask, response: AnyObject) -> Void in
            callback(response: response, error: nil)
        }
    
        let failureCallBack = { (dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            callback(response: nil, error: error)
        }
        
        if method == .GET{
            self.GET(urlString, parameters: para, success: successCallBack, failure: failureCallBack)
        }else{
            self.POST(urlString, parameters: para, success: successCallBack, failure: failureCallBack)
        }
    
    }
    
    
}

// MARK: - 首页数据请求相关
extension NetworkTools {
    
    func loadStatus(accessToken: String , sinceId: Int64 = 0,maxid:Int64 = 0  ,callback:HMNetworkToolsCallBack){
        
        let urlStr = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let paras = ["access_token" : accessToken,
                     "max_id":"\(maxid)",
                     "since_id": "\(sinceId)"
                                            ]
//        printLog(urlStr)
        
        // 请求数据
        request(.GET, urlString: urlStr, para: paras, callback: callback)
    }


}



// MARK: -OAuth相关的请求
extension NetworkTools{
// 请求access_Token
    func loadAccessToken(code:String ,callback: (response: AnyObject?, error: NSError?)->()){
    
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let params = [
            "client_id": WB_APPKEY,
            "client_secret": WB_APP_SECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": WB_REDIRECT_URI
        ]
        
        NetworkTools.shareManager.request(.POST, urlString: urlString, para: params, callback: callback)
      
    }
// 请求用户数据
    func loadUserInfo(uid: String, access_token: String, callback: (response: AnyObject?, error: NSError?)->()){
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 拼接参数
        let paras = [
            "access_token":access_token,
            "uid":uid
        ]
        // 发送请求
        NetworkTools.shareManager.request(.GET, urlString: urlString, para: paras, callback: callback)
       
    }
}

// MARK: - 发布微博相关
extension NetworkTools{

    func updata(accessToken:String,status:String,callback:(response: AnyObject?,error:NSError?)->()){
    
        let url = "https://api.weibo.com/2/statuses/update.json"
        let params = [
            "access_token": accessToken,
            "status": status
        ]
        
        request(.POST, urlString: url, para: params, callback: callback)
    
    }
    
    func upload(accessToken:String, status: String, image:UIImage,callback:(response:AnyObject?,error:NSError?)->()){
    

            
            let url = "https://upload.api.weibo.com/2/statuses/upload.json"
            let params = [
                "access_token": accessToken,
                "status": status
            ]
            let data = UIImagePNGRepresentation(image)
        
        requestUpload(url, parars: params, data: data!, name: "pic", callback: callback)
        
            
        }
    
    
    
    func requestUpload(urlString: String,parars: AnyObject?, data: NSData,name: String,callback:(response: AnyObject?, error: NSError?)->()){
    
        let succCallback = { (dataTask: NSURLSessionDataTask, response: AnyObject) -> Void in
            callback(response: response, error: nil)
        }
        
        let failureCallBack = { (dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            callback(response: nil, error: error)
        }
        
        
        
        POST(urlString, parameters: parars, constructingBodyWithBlock: { (formData) -> Void in
            //
            formData.appendPartWithFileData(data, name: name, fileName: "sss", mimeType: "application/octet-stream")
            }, success: succCallback, failure: failureCallBack)
        
        
    
    }
    
    

}